import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import MathUtil from "../../frame/codeAdapter/MathUtil";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {Logger} from "../../frame/apiModel/config/Logger";
import {SpecialEffectType} from "../../type/Type";
import {UnitType} from "../../frame/dataSource/DataModel";

export default class BigJump extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.bigJump) as string;


    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success;



        const abilityData = DataRouter.getAbilityDataById(this.abilityId);
        if (abilityData) {
        } else {
            Logger.toastProduct('大跳技能没有经过配置', activePlayer)
            return;
        }

        const range = abilityData.Area;
        const maxDistance = abilityData.Rng;
        const toSpeed = 0.01;
        const backSpeed = 0.02;



        if (activeUnit) {

            const times = 5;


            const unitArr: UnitModel[] = [];

            // ModelFactory.getInstance().mapUnitInRange(activeLoc, range,
            //     (unit) => {
            //     }
            // )

            let currentTime = 0;
            let breakFlag = false;


            let bEnd = false;

            let indexSum = 0;

            let lastLoc = activeUnit.loc;
            let lastIndex = indexSum;
            let height = 0

            const angle = lastLoc.getAngle(spellLoc);

            let bUp = true;
            let upHeight = 10

            const callBack = function () {
                if (bEnd) {
                    const loc = activeUnit.loc;
                    loc.z = 0;
                    activeUnit.loc = loc;
                    return;
                }

                indexSum = indexSum + 1
                lastIndex = lastIndex + 1;
                let locX = lastLoc.x;
                let locY = lastLoc.y;
                let locZ = lastLoc.z;
                const newX = MathUtil.toFixed(locX + (lastIndex) * 25 * MathUtil.cos(angle), 6);
                const newY = MathUtil.toFixed(locY + (lastIndex) * 25 * MathUtil.sin(angle), 6);
                height = bUp ? height + upHeight : height - upHeight;
                const newLoc = new LocModel(newX, newY, height);

                if (indexSum >= maxDistance) {
                    const delTimerOpt = new TimeControllerOpt(undefined, callBack, 1);
                    TimeController.getInstance().startTimer({time: backSpeed, opt: delTimerOpt});
                } else if (indexSum >= (maxDistance / 2)) {
                    const delTimerOpt = new TimeControllerOpt(undefined, callBack, 1);
                    TimeController.getInstance().startTimer({time: backSpeed, opt: delTimerOpt});
                }

                const timerOpt = new TimeControllerOpt(undefined, callBack, 1);
                TimeController.getInstance().startTimer({time: toSpeed, opt: timerOpt});
            }
            callBack();





            const timerOpt = new TimeControllerOpt(undefined, callBack, times);
            TimeController.getInstance().startTimer({time: toSpeed, opt: timerOpt});
        }
        return success;
    }
}